package edu.cornell.cs4740.qa.utilities;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import edu.cornell.cs4740.qa.Sentence;
import edu.cornell.cs4740.qa.WordContainer;
import edu.cornell.cs4740.qa.baseline.Answer;
import edu.cornell.cs4740.qa.ner.NamedEntityRecognizer;
import edu.cornell.cs4740.qa.pos.PosTagger;

public class Util {
  public static final String SEP = "*";

	private static NamedEntityRecognizer ner;
	private static PosTagger posTagger;
	
	public static final String walkThrough = "428";
	
	public static NamedEntityRecognizer getNer(){
		if(ner==null) ner = new NamedEntityRecognizer();
		return ner;
	}
	
	public static PosTagger getPosTagger(){
		if(posTagger==null) posTagger = new PosTagger(new File(
				"models/pos-en-general-brown.HiddenMarkovModel"));
		return posTagger;
	}
  
  public static boolean sentenceContainsQueryWord(Sentence s, Sentence q,
      boolean stemmed) {
    for (WordContainer qw : q.getAllWords()) {
      for (WordContainer sw : s.getAllWords()) {
        if (stemmed && sw.getStemmedWord().equals(qw.getStemmedWord()))
          return true;
        else if (!stemmed && sw.getWord().equals(qw.getWord()))
          return true;
      }
    }

    return false;
  }

  public static void compareAnswers(List<Answer> correctAnswers,
      List<Answer> guesses, String fileName) throws Exception {
    ArrayList<ArrayList<Answer>> groupedGuesses = new ArrayList<ArrayList<Answer>>();
    ArrayList<Answer> temp = new ArrayList<Answer>();
    String lastNum = "";

    for (Answer a : guesses) {
      if (!lastNum.equalsIgnoreCase(a.getQuestionNumber()) && !temp.isEmpty()) {
        groupedGuesses.add(temp);
        temp = new ArrayList<Answer>();
      }
      temp.add(a);
      lastNum = a.getQuestionNumber();
    }
    if (!temp.isEmpty()) {
      groupedGuesses.add(temp);
    }

    if (correctAnswers.size() != groupedGuesses.size()) {
      System.out.println(correctAnswers.size() + " correct answers, but "
          + groupedGuesses.size() + " guesses.");
      return;
    }

    // correct guess position (-1 if none)
    // correct guess text ("" if none)
    // correct answer text
    File f = new File(fileName);
    BufferedWriter bw = new BufferedWriter(new FileWriter(f));
    // 1 if correct, 0 if wrong
    // guess number
    // guess text ("" if none)
    // correct answer text
    File ff = new File("all_" + fileName);
    BufferedWriter bW = new BufferedWriter(new FileWriter(ff));

    for (int i = 0; i < correctAnswers.size(); i++) {
      Answer correct = correctAnswers.get(i);
      List<Answer> orderedGuesses = groupedGuesses.get(i);
      int j = 0;
      boolean looking = true;

      while (looking) {
        if (j < orderedGuesses.size()) {
          Answer guess = orderedGuesses.get(j);
          boolean right = isRight(guess, correct);
          if (right) {
            looking = false;
            bw.write((j + 1) + SEP + guess.getAnswerText() + SEP
                + correct.getAnswerText());
            bw.write("\n");
            bW.write("1" + SEP + (j + 1) + SEP + guess.getAnswerText() + SEP
                + correct.getAnswerText());
            bW.write("\n");
          } else {
            bW.write("0" + SEP + (j + 1) + SEP + guess.getAnswerText() + SEP
                + correct.getAnswerText());
            bW.write("\n");
          }
          j++;
        } else {
          looking = false;
          bw.write("-1" + SEP + "" + SEP + correct.getAnswerText());
          bw.write("\n");
        }
      }
    }

    bw.flush();
    bw.close();
    bW.flush();
    bW.close();
  }

  private static boolean isRight(Answer guess, Answer correct) {
    return guess.getAnswerText().toLowerCase().contains(
        correct.getAnswerText().toLowerCase());
  }

}
